using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.QQInfo.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.QQInfo.Manager
{
	public class QFriendMgr
	{
		public const string TAG = "QFriendMgr";
		private static Dictionary<int, List<QFriend>> friendListDic = new Dictionary<int, List<QFriend>>();
		private static Dictionary<int, int> weixinOnCountDic = new Dictionary<int, int>();
		public static void clearQQMemData()
		{
			QFriendMgr.friendListDic = null;
			QFriendMgr.weixinOnCountDic = null;
		}
		public static void onLogout()
		{
			QFriendMgr.clearQQMemData();
			QQInfoService.getInstance().ClearTimeStamp();
		}
		public static void SetQFriendContact(string userName, bool isContact)
		{
			QFriend qFriend = QFriendMgr.getQFriend(userName);
			if (qFriend == null)
			{
				QQInfoService.getInstance().ClearTimeStamp();
				return;
			}
			QFriendMgr.SetQfriendFlag(qFriend, isContact);
		}
		public static void SetQFriendContact(QFriend qfriend_0, bool isContact)
		{
			if (qfriend_0 != null)
			{
				QFriendMgr.SetQfriendFlag(qfriend_0, isContact);
			}
		}
		public static QFriend getQFriend(string userName)
		{
			if (QFriendMgr.friendListDic == null)
			{
				return null;
			}
			QFriend result;
			using (Dictionary<int, List<QFriend>>.Enumerator enumerator = QFriendMgr.friendListDic.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<int, List<QFriend>> current = enumerator.get_Current();
					List<QFriend> value = current.get_Value();
					if (value != null)
					{
						using (List<QFriend>.Enumerator enumerator2 = value.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								QFriend current2 = enumerator2.get_Current();
								if (current2.strUsrName == userName)
								{
									result = current2;
									return result;
								}
							}
						}
					}
				}
				goto IL_87;
			}
			return result;
			IL_87:
			return null;
		}
		public static QFriend getQFriend(uint nQQ)
		{
			if (QFriendMgr.friendListDic == null)
			{
				return null;
			}
			QFriend result;
			using (Dictionary<int, List<QFriend>>.Enumerator enumerator = QFriendMgr.friendListDic.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<int, List<QFriend>> current = enumerator.get_Current();
					List<QFriend> value = current.get_Value();
					if (value != null)
					{
						using (List<QFriend>.Enumerator enumerator2 = value.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								QFriend current2 = enumerator2.get_Current();
								if (current2.nQQ == nQQ)
								{
									result = current2;
									return result;
								}
							}
						}
					}
				}
				goto IL_84;
			}
			return result;
			IL_84:
			return null;
		}
		public static string getDisplayName(QFriend qfriend_0)
		{
			if (!string.IsNullOrEmpty(qfriend_0.strQQRemarkName))
			{
				return qfriend_0.strQQRemarkName;
			}
			if (!string.IsNullOrEmpty(qfriend_0.strQQNickName))
			{
				return qfriend_0.strQQNickName;
			}
			return qfriend_0.nQQ.ToString();
		}
		public static void update(QFriend item)
		{
			StorageMgr.qfriend.update(item);
		}
		public static void updateListRank(QFriend item, bool isFriend)
		{
			QFriend qFriend = new QFriend();
			int num = 0;
			int nGroupID = item.nGroupID;
			if (QFriendMgr.friendListDic == null || QFriendMgr.weixinOnCountDic == null)
			{
				return;
			}
			if (!QFriendMgr.friendListDic.ContainsKey(nGroupID))
			{
				return;
			}
			while (num < QFriendMgr.friendListDic.get_Item(nGroupID).get_Count() && QFriendMgr.friendListDic.get_Item(nGroupID).get_Item(num).nQQ != item.nQQ)
			{
				num++;
			}
			Log.smethod_5("QFriendMgr", string.Concat(new object[]
			{
				"this friend's rank is ",
				num,
				" , the count of this group is ",
				QFriendMgr.friendListDic.get_Item(nGroupID).get_Count()
			}));
			if (num == QFriendMgr.friendListDic.get_Item(nGroupID).get_Count())
			{
				return;
			}
			if (isFriend)
			{
				qFriend = QFriendMgr.friendListDic.get_Item(nGroupID).get_Item(num);
				QFriendMgr.friendListDic.get_Item(nGroupID).set_Item(num, QFriendMgr.friendListDic.get_Item(nGroupID).get_Item(QFriendMgr.weixinOnCountDic.get_Item(nGroupID) - 1));
				QFriendMgr.friendListDic.get_Item(nGroupID).set_Item(QFriendMgr.weixinOnCountDic.get_Item(nGroupID) - 1, qFriend);
				Dictionary<int, int> dictionary;
				int num2;
				(dictionary = QFriendMgr.weixinOnCountDic).set_Item(num2 = nGroupID, dictionary.get_Item(num2) - 1);
				return;
			}
			qFriend = QFriendMgr.friendListDic.get_Item(nGroupID).get_Item(num);
			QFriendMgr.friendListDic.get_Item(nGroupID).set_Item(num, QFriendMgr.friendListDic.get_Item(nGroupID).get_Item(QFriendMgr.weixinOnCountDic.get_Item(nGroupID)));
			QFriendMgr.friendListDic.get_Item(nGroupID).set_Item(QFriendMgr.weixinOnCountDic.get_Item(nGroupID), qFriend);
			Dictionary<int, int> dictionary2;
			int num3;
			(dictionary2 = QFriendMgr.weixinOnCountDic).set_Item(num3 = nGroupID, dictionary2.get_Item(num3) + 1);
		}
		public static List<QFriend> getSortedQFriendList(int groupID)
		{
			List<QFriend> list = StorageMgr.qfriend.getListByGroupID(groupID);
			if (list != null && list.get_Count() != 0)
			{
				if (QFriendMgr.friendListDic == null)
				{
					QFriendMgr.friendListDic = new Dictionary<int, List<QFriend>>();
					QFriendMgr.weixinOnCountDic = new Dictionary<int, int>();
				}
				if (!QFriendMgr.friendListDic.ContainsKey(groupID))
				{
					list.Sort(new Comparison<QFriend>(QFriendExtensions.QFriendCompare));
					QFriendMgr.friendListDic.Add(groupID, list);
					QFriendMgr.weixinOnCountDic.Add(groupID, 0);
					QFriendMgr.computeRecordWeixinOnFriend(groupID, list);
				}
				else
				{
					list = QFriendMgr.friendListDic.get_Item(groupID);
				}
				return list;
			}
			return null;
		}
		public static List<QFriend> getQFriendList(int groupID, int start, int count)
		{
			List<QFriend> sortedQFriendList = QFriendMgr.getSortedQFriendList(groupID);
			if (sortedQFriendList == null)
			{
				return sortedQFriendList;
			}
			List<QFriend> list = new List<QFriend>();
			int num = 0;
			while (num < count && start + num < sortedQFriendList.get_Count())
			{
				list.Add(sortedQFriendList.get_Item(start + num));
				num++;
			}
			return list;
		}
		private static void computeRecordWeixinOnFriend(int groupID, List<QFriend> friendList)
		{
			int num = 0;
			while (num < friendList.get_Count() && friendList.get_Item(num).nWeixinStatus == 1u)
			{
				num++;
			}
			if (QFriendMgr.weixinOnCountDic == null)
			{
				QFriendMgr.weixinOnCountDic = new Dictionary<int, int>();
			}
			if (QFriendMgr.weixinOnCountDic.ContainsKey(groupID))
			{
				QFriendMgr.weixinOnCountDic.set_Item(groupID, num);
				return;
			}
			QFriendMgr.weixinOnCountDic.Add(groupID, num);
		}
		private static void SetQfriendFlag(QFriend qfriend_0, bool isContact)
		{
			if (qfriend_0 == null)
			{
				return;
			}
			if (isContact)
			{
				if (qfriend_0.nWeixinStatus == 2u)
				{
					return;
				}
				qfriend_0.nWeixinStatus = 2u;
			}
			else
			{
				if (qfriend_0.nWeixinStatus == 1u)
				{
					return;
				}
				qfriend_0.nWeixinStatus = 1u;
			}
			QFriendMgr.update(qfriend_0);
			QFriendMgr.updateListRank(qfriend_0, isContact);
		}
	}
}
